package ui.components.ebook
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	
	import global.Managers;
	
	import resources.Resources;
	
	import utils.EnterFrameTimer;
	import utils.pool.ObjectPool;
	
	import worlds.BaseWorld;

	/**
	 *
	 * 电子书组件 
	 * @author Raykid
	 * 
	 */
	public class EBook extends Sprite
	{
		public static const SWITCH_PREV			: int = 1;
		public static const SWITCH_NEXT			: int = 2;
		public static const ZOOM_OUT			: int = 3;
		public static const ZOOM_IN				: int = 4;
		
		private static const TIME_ANIMATE		: int = 500;		// 执行一次翻页需要的毫秒数
		private static const ZOOM_MULTIPLE		: int = 2;			// 放大的倍数
		
		private var _timer						: EnterFrameTimer;
		private var _pageContainer				: Sprite;
		private var _currentPageLeft			: EBookPage;
		private var _currentPageRight			: EBookPage;
		private var _reservePageLeft			: EBookPage;
		private var _reservePageRight			: EBookPage;
		
		private var _animateType				: int;				// 动画类型
		private var _zoomState					: Boolean = false;	// 为true表示ebook处于放大的状态
		
		private var _pages						: Array = [];
		private var _currentPage				: int;
		private var _width						: Number;
		private var _height						: Number;
		private var _zoomInX					: Number;
		private var _zoomInY					: Number;
		
		public function get currentPage():int {
			return _currentPage;
		}
		public function get numPages():int {
			return ((_pages.length + 2) / 2);
		}
		public function get zoomState():Boolean {
			return _zoomState;
		}
		
		public function EBook(width:Number, height:Number)
		{
			_width = width;
			_height = height;
			super();
			init();
		}
		private function init():void {
			// 执行周期跟随程序帧频即可
			var delay:Number = 1000 / BaseWorld.getInstance().stage.frameRate;
			var times:int = int(TIME_ANIMATE / delay);
			_timer = new EnterFrameTimer(delay, times);
			_timer.addEventListener(TimerEvent.TIMER, timerHandler);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
			// 初始化页面容器
			_pageContainer = new Sprite();
			addChild(_pageContainer);
			
			// 初始化四个页面
			_currentPageLeft = new EBookPage(_width / 2, _height);
			_currentPageLeft.setSize(_width / 2, _height, true);
			_pageContainer.addChild(_currentPageLeft);
			
			_currentPageRight = new EBookPage(_width / 2, _height);
			_currentPageRight.setSize(_width / 2, _height, true);
			_pageContainer.addChild(_currentPageRight);
			
			_reservePageLeft = new EBookPage(_width / 2, _height);
			_pageContainer.addChild(_reservePageLeft);
			
			_reservePageRight = new EBookPage(_width / 2, _height);
			_pageContainer.addChild(_reservePageRight);
		}
		private function timerHandler(event:TimerEvent):void {
			var currentCount:Number = _timer.currentCount;
			var repeatCount:Number = _timer.repeatCount;
			var leftCount:Number = repeatCount - currentCount;
			var distance:Number;
			var zoomMultiple:Number;
			switch(_animateType) {
				case SWITCH_PREV:
					distance = currentCount * ((_width / 2) / repeatCount);
					_currentPageLeft.setSize(_width / 2 - distance * 2, _height, false);
					_reservePageLeft.setSize(distance, _height, true);
					_reservePageRight.setSize(distance, _height, false);
					
					_reservePageLeft.x = 0;
					_reservePageRight.x = distance * 2 - _width / 2;
					
					break;
				case SWITCH_NEXT:
					distance = currentCount * ((_width / 2) / repeatCount);
					_currentPageRight.setSize(_width / 2 - distance * 2, _height, true);
					_reservePageLeft.setSize(distance, _height, true);
					_reservePageRight.setSize(distance, _height, false);
					
					_reservePageLeft.x = _width - distance * 2;
					_reservePageRight.x = _width / 2;
					
					break;
				case ZOOM_OUT:
					zoomMultiple = (ZOOM_MULTIPLE - 1) / repeatCount;
					_pageContainer.scaleX -= zoomMultiple;
					_pageContainer.scaleY -= zoomMultiple;
					
					_pageContainer.x -= _pageContainer.x / leftCount;
					_pageContainer.y -= _pageContainer.y / leftCount;
					
					break;
				case ZOOM_IN:
					zoomMultiple = (ZOOM_MULTIPLE - 1) / repeatCount;
					_pageContainer.scaleX += zoomMultiple;
					_pageContainer.scaleY += zoomMultiple;
					
					var temp:Number;
					var scale:Number = _pageContainer.scaleX;
					temp = (_width / 2 - _zoomInX) * ZOOM_MULTIPLE / repeatCount * (currentCount + 1) - (_width / 2) * (scale - 1);
					if(temp < -(_width * (scale - 1))) temp = -(_width * (scale - 1));
					if(temp > 0) temp = 0;
					_pageContainer.x = temp;
					
					temp = (_height / 2 - _zoomInY) * ZOOM_MULTIPLE / repeatCount * (currentCount + 1) - (_height / 2) * (scale - 1);
					if(temp < -(_height * (scale - 1))) temp = -(_height * (scale - 1));
					if(temp > 0) temp = 0;
					_pageContainer.y = temp;
					
					break;
				default:
					_timer.stop();
					return;
			}
		}
		private function timerCompleteHandler(event:TimerEvent):void {
			updateView();
		}
		private function updateView():void {
			// 设置左页面
			_currentPageLeft.setContent(_pages[_currentPage * 2 - 1]);
			_currentPageLeft.setSize(_width / 2, _height, true);
			_currentPageLeft.x = 0;
			_currentPageLeft.y = 0;
			// 设置右页面
			_currentPageRight.setContent(_pages[_currentPage * 2]);
			_currentPageRight.setSize(_width / 2, _height, true);
			_currentPageRight.x = _width / 2;
			_currentPageRight.y = 0;
			// 设置备用左页面
			_reservePageLeft.clear();
			// 设置备用右页面
			_reservePageRight.clear();
			// 根据放大状态设置大小和位置
			if(_zoomState) {
				_pageContainer.scaleX = ZOOM_MULTIPLE;
				_pageContainer.scaleY = ZOOM_MULTIPLE;
			} else {
				_pageContainer.scaleX = 1;
				_pageContainer.scaleY = 1;
				_pageContainer.x = 0;
				_pageContainer.y = 0;
			}
		}
		
		/**
		 *
		 * 设置内容，该方法会覆盖原有内容 
		 * @param pages 各页面内容，各页面均为DisplayObject或其子类
		 * 
		 */		
		public function update(pages:Array):void {
			while(_pages.length > 0) {
				var page:DisplayObject = _pages.pop();
				ObjectPool.push(page);
			}
			_pages = pages;
			// 如果页数是奇数个，则在最后一页使用默认图像填充
			if(_pages.length % 2 == 1) {
				_pages.push(Managers.resourceManager.getBitmap(Resources.RECIPE_NO_PAGE));
			}
			_currentPage = 0;
			updateView();
		}
		/**
		 *
		 * 放大电子书 
		 * 
		 */		
		public function zoomIn(x:Number = 0, y:Number = 0):void {
			if(_zoomState) return;
			if(_timer.running) updateView();
			_animateType = ZOOM_IN;
			_zoomState = true;
			_zoomInX = x;
			_zoomInY = y;
			_timer.reset();
			_timer.start();
		}
		/**
		 *
		 * 缩小电子书 
		 * 
		 */		
		public function zoomOut():void {
			if(!_zoomState) return;
			if(_timer.running) updateView();
			_animateType = ZOOM_OUT;
			_zoomState = false;
			_timer.reset();
			_timer.start();
		}
		/**
		 *
		 * 在放大状态时移动电子书 
		 * @param x 移动的横向距离
		 * @param y 移动的纵向距离
		 * 
		 */		
		public function move(x:Number = 0, y:Number = 0):void {
			if(!_zoomState) return;
			if(x == 0 && y == 0) return;
			
			var temp:Number;
			// 设置X
			temp = _pageContainer.x + x;
			if(temp < -(_width * (ZOOM_MULTIPLE - 1))) temp = -(_width * (ZOOM_MULTIPLE - 1));
			if(temp > 0) temp = 0;
			_pageContainer.x = temp;
			// 设置Y
			temp = _pageContainer.y + y;
			if(temp < -(_height * (ZOOM_MULTIPLE - 1))) temp = -(_height * (ZOOM_MULTIPLE - 1));
			if(temp > 0) temp = 0;
			_pageContainer.y = temp;
		}
		/**
		 *
		 * 跳转到指定页数 
		 * @param page 指定的页数
		 * @param hasAnimate 是否需要动画
		 * 
		 */		
		public function goToPage(page:int, hasAnimate:Boolean = true):void {
			if(page < 0 || page > numPages - 1 || page == _currentPage) return;
			_animateType = (page < _currentPage) ? SWITCH_PREV : SWITCH_NEXT;
			// 设置原始2个界面内容和大小
			_currentPageLeft.setContent(_pages[(_currentPage) * 2 - 1]);
			_currentPageRight.setContent(_pages[(_currentPage) * 2]);
			_currentPageLeft.setSize(_width / 2, _height, true);
			_currentPageRight.setSize(_width / 2, _height, true);
			// 更新当前页数
			_currentPage = page;
			// 设置新的2个界面内容
			_reservePageLeft.setContent(_pages[(_currentPage) * 2 - 1]);
			_reservePageRight.setContent(_pages[(_currentPage) * 2]);
			// 开启动画
			_timer.reset();
			_timer.start();
		}
		/**
		 *
		 * 向前翻页 
		 * @param hasAnimate 是否需要动画
		 * 
		 */		
		public function prevPage(hasAnimate:Boolean = true):void {
			goToPage(_currentPage - 1, hasAnimate);
		}
		/**
		 *
		 * 向后翻页 
		 * @param hasAnimate 是否需要动画
		 * 
		 */		
		public function nextPage(hasAnimate:Boolean = true):void {
			goToPage(_currentPage + 1, hasAnimate);
		}
		/**
		 *
		 * 重置电子书到第一页 
		 * 
		 */		
		public function reset():void {
			_currentPage = 0;
			_zoomState = false;
			updateView();
		}
	}
}